<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"-->
    <el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
              style="width: 100%;">
      <!--多选框-->
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column type="index" label="序号" fixed>
      </el-table-column>
      <el-table-column prop="articlePic"  label="文章封面" width="120" >
        <template scope="scope">
          <el-image
              :src="scope.row.articlePic"
              fit="cover">
          </el-image>
          <img  height="50px"/>
        </template>
      </el-table-column>
      <el-table-column prop="articleName" fixed label="文章名称" >
      </el-table-column>
      <el-table-column prop="articleTag" fixed label="文章标签" >
      </el-table-column>
      <el-table-column prop="typeName" label="文章类型" >
      </el-table-column>
      <el-table-column prop="articleState" label="文章状态">
        <template scope="scope">
          <span v-if="scope.row.articleState==0" style="color: coral;">草稿</span>
          <span v-if="scope.row.articleState==1" style="color: green;">发布</span>
          <span v-if="scope.row.articleState==2" style="color: gray;">弃用</span>
        </template>
      </el-table-column>
      <el-table-column prop="topState" label="是否置顶" >
          <template scope="scope">
            <div v-if="!scope.row.topState" style="color: red;">不置顶</div>
            <div v-if="scope.row.topState" style="color: cyan;">置顶</div>
          </template>
      </el-table-column>
      <el-table-column prop="realName" label="发布人">
      </el-table-column>
      <el-table-column prop="createTime" label="发布时间"  width="150">
      </el-table-column>
<!--      <el-table-column prop="updateTime" label="修改时间" >-->
<!--      </el-table-column>-->
      <el-table-column prop="articleReadCount" label="阅读量" >
      </el-table-column>
      <el-table-column prop="articleStarNum" label="点赞数">
      </el-table-column>
      <el-table-column prop="articleCommentNum" label="评论数">
      </el-table-column>
<!--      <el-table-column prop="articleRemark" label="文章简介">-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="articleContent" label="文章内容" show-overflow-tooltip>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--添加或修改界面-->
    <el-dialog title="保存文章信息" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm " label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="文章类型" prop="articleType">
            <el-select v-model="saveForm.articleType" clearable placeholder="请选择文章类型">
              <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.typeName"
                  :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="文章名称" prop="articleName">
          <el-input v-model="saveForm.articleName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章标签" prop="articleTag">
          <el-input v-model="saveForm.articleTag" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章状态" prop="articleState">
<!--          <el-input v-model="saveForm.articleState" auto-complete="off"></el-input>-->
          <template>
            <el-radio v-model="saveForm.articleState" :label="0">草稿</el-radio>
            <el-radio v-model="saveForm.articleState" :label="1">发布</el-radio>
            <el-radio v-model="saveForm.articleState" :label="2">弃用</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="是否置顶">
<!--          <el-input v-model="saveForm.topState" auto-complete="off"></el-input>-->
          <template>
            <el-radio v-model="saveForm.topState" :label="0">不置顶</el-radio>
            <el-radio v-model="saveForm.topState" :label="1">置顶</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="文章封面" prop="articlePic">
<!--          <el-input v-model="saveForm.articlePic" auto-complete="off"></el-input>-->
          <!--
          action 请求地址
          on-exceed 限制上传个数
          file-list 图片上传的信息
          limit 限制上传个数
          accept 允许上传的类型
           -->
            <el-upload
                action="http://localhost:8080/minio"
                list-type="picture-card"
                name="file"
                :on-success="handleSuccess"
                :file-list="fileList"
                accept=".jpg,.jpeg,.png,.gif"
                :limit=1
                :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
        </el-form-item>
        <el-form-item label="文章简介" prop="articleRemark">
          <el-input type="textarea" :rows="3" v-model="saveForm.articleRemark"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleContent">
          <quill-editor v-model="saveForm.articleContent"
                        :options="editorOption"
                        @ready="onEditorReady($event)">
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
//引入Vue的ueditor的资源
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ["blockquote", "code-block"], // 引用  代码块-----['blockquote', 'code-block']
  [{header: 1}, {header: 2}], // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
  [{list: "ordered"}, {list: "bullet"}], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{script: "sub"}, {script: "super"}], // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
  [{indent: "-1"}, {indent: "+1"}], // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
  [{direction: "rtl"}], // 文本方向-----[{'direction': 'rtl'}]
  [{size: ["small", false, "large", "huge"]}], // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
  [{header: [1, 2, 3, 4, 5, 6, false]}], // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
  [{color: []}, {background: []}], // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
  [{font: []}], // 字体种类-----[{ font: [] }]
  [{align: []}], // 对齐方式-----[{ align: [] }]
  ["clean"], // 清除文本格式-----['clean']
  ["image", "link"] // 链接、图片、视频-----['link', 'image', 'video']
];
import el from "element-ui/src/locale/lang/el";

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      exceed:1,
      fileList:[],
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      //富文本编辑器配置
      editorOption: {
        modules: {
          //工具栏定义的
          toolbar: toolbarOptions
        },
        //主题
        theme: "snow",
        placeholder: "请开始书写您的博客内容..."
      },
      options: [],
      //1.分页查询相关数据
      totals: 0,//总记录条数
      currentPage: 1,//当前页,要传递到后台的
      pageSize: 5, //每页显示多少条
      tableDatas: [], //当前页数据
      listLoading: false,

      //2.高级查询相关数据
      keyword: '',

      //3.删除相关数据

      //4.批量删除相关数据
      sels: [], //存储多选记录

      //5.添加和修改相关数据
      saveFormVisible: false,
      saveForm: {
        id: null,
        articlePic: null,
        articleName: null,
        articleTag: null,
        articleType: null,
        articleState: null,
        topState: null,
        publishId: null,
        createTime: null,
        updateTime: null,
        articleReadCount: null,
        articleStarNum: null,
        articleCommentNum: null,
        articleRemark: null,
        articleContent: null,
      },

      //6.添加和修改校验规则-表单项校验
      saveFormRules: {
        articleName: [
          { required: true, message: '请输入文章名称', trigger: 'blur' }
        ],
        articleType: [
          { required: true, message: '请选择文章类型', trigger: 'blur' }
        ],
        articleRemark: [
          { required: true, message: '请输入文章介绍', trigger: 'blur' }
        ],
        articleContent: [
          { required: true, message: '请选择文章内容', trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    //删除
    handleRemove(file) {
      console.log(file.data);
    },
    //上传成功后加载的方法
    handleSuccess(file,filelist) {
      // console.log(file.data);
      this.saveForm.articlePic = file.data;
      // console.log(filelist);
    },
    onEditorReady(editor) {
      //当富文本编辑框初始化好执行
      // console.log('富文本编辑框初始化---------完成');
    },
    //获取表格数据
    getTableData(){
      var data = {
        "page":this.currentPage,
        "size":this.pageSize,
        "keyword":this.keyword
      }
      this.$http.post("/article/page",data).then(res=>{
        // console.log(res)
        if(res.data.success){
          this.tableDatas = res.data.data.list
          this.totals = res.data.data.total
        }else {
          this.$message(res.data.msg);
        }
      }).catch(res=>{
        console.log(res)
      })
    },
    //修改
    handleEdit(row){
      // console.log(val)
      this.saveFormVisible = true
      this.saveForm = Object.assign({}, row);
      //添加先清空
      this.fileList = [];
      var url = this.saveForm.articlePic
      var urlArr = url.split("/");
      // console.log(urlArr)
      var fileName = urlArr[urlArr.length-1]
      console.log(fileName)
      //存储到fileList中
      this.fileList.push({
        name:fileName,
        url:url
      })
    },
    //删除
    handleDel(val){
      // console.log(val.id)
      var id = val.id
      this.$alert('确定要删除？', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if(action === 'confirm'){
            this.$http.delete("/article/"+id).then(res=>{
              if(res.data.success){
                this.getTableData()
                this.$message({
                  type: 'info',
                  message: `action: ${action}`
                });
              }else {
                this.$message(res.data.msg);
              }
              // console.log(res)
            }).catch(res=>{
              console.log(res)
            })
          }
        }
      })
    },
    //批量删除
    handlePatchDel(){
      this.$alert('确定要删除'+this.sels+'吗?', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if(action === 'confirm') {
            this.$http.post("/article/delBatch", this.sels).then(res => {
              // console.log(res)
              if (res.data.success) {
                this.getTableData()
                this.$message({
                  type: 'info',
                  message: `action: ${action}`
                })
              } else {
                this.$message(res.data.msg);
              }
            }).catch(res=>{
              console.log(res)
            })
          }
        }
      });
    },
    handleSelectionChange(val){
      this.sels = val.map(a=>a.id)
      // console.log(this.sels)
    },
    //记录条数
    handleSizeChange(pageSize){
      this.pageSize =pageSize
      this.getTableData()
    },
    //记录起始页
    handleCurrentChange(currentPage){
      this.currentPage = currentPage
      this.getTableData()
    },
    //修改和添加按钮
    saveSubmit(){
      // console.log(this.saveForm)
      var data1 = this.saveForm
      // console.log(data)
      this.$http.post("/article",data1).then(res=>{
        // console.log(res)
        if(res.data.success){
          this.saveFormVisible = false
          this.getTableData()
        }else {
          this.$message(res.data.msg);
        }
      }).catch(res=>{
        console.log(res)
      })
    },
    //查询关键字
    keywordQuery(){
      this.getTableData()
    },
    //新增
    handleAdd(){
      this.saveFormVisible = true
      this.saveForm = {
        articleState:1,
        topState:1,
      }
      //添加先清空
      this.fileList = []
    },
    //获取文章类型
    getArticleType(){
      this.$http.get("/articleType").then(res=>{
        // console.log(res)
        if(res.data.success){
          // console.log(res)
          this.options = res.data.data
        }else {
          this.$message = res.data.msg
        }
      }).catch(res=>{
        console.log(res)
      })
    }
  },
  mounted() {
    this.getTableData();
    this.getArticleType()
  }
}

</script>

<style scoped></style>
